<?php
if (!defined('IN_XLP')) {
    exit('Access Denied');
}
$Document = array(
    'pageid' => 'news-number', //页面标示
    'pagename' => '常用号码', //当前页面名称
    'mycss' => array('content/textlist'), //加载的css样式表
    'myjs' => array(), //加载的js脚本
    'footerjs'=>array('content/fuelux/fuelux', 'content/combodate/moment.min', 'content/jquery.smoothConfirm'),
    'head' => true, //加载头部文件
);
$total=1;
include getTpl('header', 'public');
?>
<!--顶部导航 开始-->
<?php include getTpl('top', 'public');?>
<!--顶部导航 结束-->
<!--左侧菜单 开始-->
<?php include getTpl('nav', 'public');?>
<!--左侧菜单 结束-->
<!--主体 开始-->
<!-- content -->

<section id="content">
  <div class="main padder">
    <div class="clearfix">
      <h4><i class="fa fa-edit"></i>常用号码</h4>
    </div>
    <div class="row">
      <div class="col-sm-12">
        <div class="panel">
          <div class="panel-body"> 
            <!--内容 B-->
            <form class="form-horizontal" id="noticeAddForm" method="post">
              <div class="form-group">
                <button type="button" id="add-number" class="btn btn-primary m-l-large">添加号码</button>
              </div>
              <div class="col-sm-12" id="number-content" style="padding-left:0;">
                <?php
							if ($rs){
                            foreach($rs as $val){
							?>
                <div class="form-group number-item" id="number-<?php echo $total;?>">
                  <div class="col-sm-3">
                    <select name="picture" class="form-control" style="width:70%; margin-top:10px;">
                      <option value="0">默认图标</option>
                      <?php
                                        foreach($numberType as $k=>$v){
										?>
                      <option value="<?php echo $k;?>" <?php if ($val['picture']==$k){echo 'selected="selected"';}?>><?php echo $v;?></option>
                      <?php
										}
										?>
                    </select>
                  </div>
                  <div class="col-sm-3">
                    <input type="text" name="name" placeholder="服务岗位（必填）" required="true" class="form-control" style="width:70%;margin-top:10px;" value="<?php echo $val['post'];?>">
                  </div>
                  <div class="col-sm-3">
                    <input type="text" name="phone" placeholder="电话号码（必填）" required="true" class="form-control" style="width:70%;margin-top:10px;" value="<?php echo $val['phone'];?>">
                  </div>
                  <div class="col-sm-2">
                    <button type="button" class="btn btn-primary btn-danger delete-btn" style="margin-top:10px;">删除</button>
                  </div>
                </div>
                <?php
				   $total+=1;
				}
				}else{
				   ?>
                <div class="form-group number-item" id="number-<?php echo $total;?>">
                  <div class="col-sm-3">
                    <select name="picture" class="form-control" style="width:70%; margin-top:10px;">
                      <option value="0">请选择图标</option>
                      <?php
						foreach($numberType as $k=>$v){
						?>
	  <option value="<?php echo $k;?>"><?php echo $v;?></option>
	  <?php
						}
						?>
                    </select>
                  </div>
                  <div class="col-sm-3">
                    <input type="text" name="name" placeholder="服务岗位（必填）" required="true" class="form-control" style="width:70%;margin-top:10px;">
                  </div>
                  <div class="col-sm-3">
                    <input type="text" name="phone" placeholder="电话号码（必填）" required="true" class="form-control" style="width:70%;margin-top:10px;">
                  </div>
                  <div class="col-sm-2">
                    <button type="button" class="btn btn-primary btn-danger delete-btn" style="margin-top:10px;">删除</button>
                  </div>
                </div>
                <?php
							}
							?>
              </div>
              <div class="form-group">
                <div class="col-sm-4">
                  <button type="button" id="sub-ok" data-loading-text="正在提交..." class="btn btn-primary">保存</button>
                  <button type="button" class="btn btn-white col-md-offset-1">取消</button>
                </div>
              </div>
            </form>
            <!--内容 E--> 
          </div>
        </div>
      </div>
    </div>
  </div>
  </div>
</section>
<!--/ content --> 
<!--主体 结束--> 
<script>
$(function(){
	var numId = <?php echo $total;?>;
	var postUrl='<?php echo U('news/savenumber');?>';
	//添加号码
	$("#add-number").click(function(){
		var $lastItem = $(".number-item:last");
        var $newItem = $lastItem.clone();
		numId++;
		$newItem.attr("id","number-"+numId);
		$("input", $newItem).val("");
		$lastItem.after($newItem);
	});
	
	//删除号码
	$(document).on("click",".delete-btn",function(){
		console.log("删除号码");
		var $this = $(this);
		var numItem = $this.parents(".form-group");
		var numItemId = numItem.attr("id");
		$this.smoothConfirm("确认删除此行吗？", {
			okVal: "确认",
			cancelVal: '取消',
			ok: function() {
				numItem.remove();
			},
			cancel: function() {
				return false;
			}
		});
		return false;
	});
	
	$("#sub-ok").click(function(){
		var isOk=true;
		var jsonData = {};
		$(".number-item").each(function(i, e) {
            var nItem = $(e);
			var pic = $("select[name='picture']",nItem).val();
			var post = $("input[name='name']",nItem).val();
			var phone = $("input[name='phone']",nItem).val();
			if (!post) {
                Msg.error("岗位不能为空");
				isOk=false;
				 resetSubmit("#sub-ok",'保存');
                return false;
            }
           if (!phone) {
                Msg.error("电话不能为空");
				isOk=false;
				resetSubmit("#sub-ok",'保存');
                return false;
            }

			jsonData[i] = {
                picture: pic,
                post: post,
                phone: phone
            };
        });
		if (!isOk){
			return false;	
		}
		Msg.loading();
		$.post(postUrl,{"data":jsonData},function(result){
			Msg.hide();
			 resetSubmit("#sub-ok",'保存');
			if (result.status==1){
				Msg.ok('保存成功');
			}else{
				Msg.error(result.info);
			}	
		},'json');
		return false;
	});
})
</script>
<?php
include getTpl('footer', 'public');
?>
